package org.springboot.mvc;

import org.springboot.jdo.WorkingHours;
import org.springboot.service.WorkingHoursService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 计件工时管理.
 * <p>
 * Created by goldendba@gmail.com on 2017/3/3.
 */
@Controller
@RequestMapping("/workinghours")
public class WorkingHoursController {
    @Autowired
    private WorkingHoursService workingHoursService;

    @RequestMapping("/edit_view")
    public String editView() {
        return "workinghours/edit";
    }

    /**
     * 新增工时.
     *
     * @param workingHours
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public String post(WorkingHours workingHours) {
        workingHoursService.save(workingHours);
        return "workinghours/list";
    }

    /**
     * 修改工时.
     *
     * @param id
     * @param workingHours
     * @return
     */
    @RequestMapping(value = "/edit/{id}", method = RequestMethod.PUT)
    public String put(@PathVariable Long id, @ModelAttribute WorkingHours workingHours) {
        WorkingHours single = workingHoursService.findById(id);
        workingHoursService.save(single);
        return "workinghours/list";
    }

    /**
     * 删除工时.
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/del/{id}", method = RequestMethod.DELETE)
    public String delete(@PathVariable Long id) {
        workingHoursService.del(id);
        return "workinghours/list";
    }

    @RequestMapping("/list_view")
    public String listView() {
        return "workinghours/list";
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public List<WorkingHours> list() {
        return workingHoursService.list();
    }
}
